package com.hll.servlet;

import com.hll.utils.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: JavaWebDemo-01
 * @description:
 * @author: Mr.Hu
 * @create: 2021-03-06 20:03
 */
public class Login extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String name = req.getParameter("name");
    if (name.equals(Constant.USER_NAME)) {
      // 暂时先根据session有这个属性代表已登入
      req.getSession().setAttribute(Constant.LOGIN_ATTRIBUTE, req.getSession().getId());
      /*
      又遇到怪事了,resp.sendRedirect("/sys/success.jsp"); 404错误,地址栏上跳转的地址是
      localhost/f/servlet/sys/success.jsp
      注： /f 是项目名, servlet/login  是处理登入的servlet
      而success.jsp 在 webapp/sys/ 下

      因为之前的servlet mapping都是 /XX 的,我现在的是 /servlet/XX 所以可能跟这个有关系(亲测，的确与这个有关)
      所以以后像下面这么写就行, 但好像 getRequestDispatcher 却不需要
      * */
      resp.sendRedirect(req.getContextPath() + "/sys/success.jsp");
    } else {
      resp.sendRedirect(req.getContextPath() + "/sys/error.jsp");
    }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doGet(req, resp);
  }
}
